package newsParser;

/**
 * 
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import functions.DoubleValueComparator;
import functions.KomoranAnalyzer;

/**
 * @author Jonghyun Han
 *
 */
public class NewsNounParser {
	
	private String category_start;
	private String subCategory_start;
	private String date_start;
	
	
	private Boolean isStart;
	private Boolean isStart2;
	KomoranAnalyzer kom;
	public NewsNounParser(){
		kom = new KomoranAnalyzer();
	}

	public void setStartpoint(Boolean b){
		isStart = b;
	}
	public void setStartpoint(String category, String subCategory){
		isStart = true;
		isStart2 = false;
		category_start = category;
		subCategory_start = subCategory;
	}
	public void setStartpoint(String category, String subCategory, String date){
		isStart = true;
		isStart2 = true;
		category_start = category;
		subCategory_start = subCategory;
		date_start = date;
	}
	
	public void set(String category, String subCategory){
		category_start = category;
		subCategory_start = subCategory;
	}
	
	public void ExtractNaverNewsNoun(String newsRootDirectory, String saveRootDirectory, String tagger) {		
		List<File> dirCategories = NewsFileReader.getListOfSubDirectories(newsRootDirectory);
		for (File category : dirCategories) {
			List<File> dirSubCategories = NewsFileReader.getListOfSubDirectories(category.getAbsolutePath());
			for (File subCategory : dirSubCategories) {
				System.out.println(category.getName() + "\t" + subCategory.getName());
				
				if(isStart){
					if(category.getName().equals(category_start) && subCategory.getName().equals(subCategory_start)){
						isStart = false;
					}else{
						continue;
					}
				}
				
				new File(saveRootDirectory + category.getName() + "\\" + subCategory.getName()).mkdirs();
				
				List<File> dates = NewsFileReader.getListOfFiles(subCategory.getAbsolutePath());
				int printct=0;
				for (File date : dates) {
					
					if( printct >= 5000 ){
						System.out.println(date.getAbsolutePath());
						printct = 0;
					}else printct++;
					
					String fileContent = NewsFileReader.getNewsContent(date.getAbsolutePath()).trim();
					ArrayList<String> nlists = NewsFileReader.getEachNewsContent(date.getAbsolutePath());
					
					fileContent = fileContent.replaceAll("[^\uAC00-\uD7A3xfe0-9a-zA-Z\\s.]"," ");

					//HashMap<String, Integer> nounCounts = nounCounter.GetNounCounts(fileContent);
					HashMap<String, Double> nounCounts;
					if(tagger.equals("Komoran-v2.0")){
						int newsct = 1;
						for(String nlist : nlists){
							//String nlistStr = nlist.replaceAll("[^\uAC00-\uD7A3xfe0-9a-zA-Z\\s.]"," ");
							String nlistStr = nlist.replaceAll("[\\s]", " ").replaceAll("\\s{2,}"," ");
							System.out.println(nlistStr);
							
							nounCounts = kom.GetNounCounts(nlistStr, "1");
							
							SaveWordCount(nounCounts, saveRootDirectory + category.getName() + "\\" + subCategory.getName() + "\\" + date.getName().substring(0, 8) + "_" + newsct + " WordCount.txt");
							newsct++;
						}
					}else if(tagger.equals("Komoran-v2.0-NNP")){
						//nounCounts = kom.GetNounCounts(fileContent, "2");
						//SaveWordCount(nounCounts, saveRootDirectory + category.getName() + "\\" + subCategory.getName() + "\\" + date.getName().substring(0, 8) + " WordCount.txt");						
						int newsct = 1;
						for(String nlist : nlists){
							String nlistStr = nlist.replaceAll("[^\uAC00-\uD7A3xfe0-9a-zA-Z\\s.]"," ");
							nlistStr = nlist.replaceAll("[\\s]", " ").replaceAll("\\s{2,}"," ");
							
							nounCounts = kom.GetNounCounts(nlistStr, "2");
							SaveWordCount(nounCounts, saveRootDirectory + category.getName() + "\\" + subCategory.getName() + "\\" + date.getName().substring(0, 8) + "_" + newsct + " WordCount.txt");
							newsct++;
						}

					}
						
				}
				
			} 
			
			dirSubCategories.clear();
		}
	}
	
	public void SaveWordCount(Map<String, Double> map, String filename) {
		//System.out.println(filename);
		try {
			FileWriter fw = new FileWriter(filename);
			BufferedWriter writer = new BufferedWriter(fw);
		
			DoubleValueComparator doubleValueComparator = new DoubleValueComparator(map);
			TreeMap<String, Double> treeMap = new TreeMap<String, Double>(doubleValueComparator);
			treeMap.putAll(map);

			for (Map.Entry<String, Double> entry : treeMap.entrySet()) {
				writer.write(entry.getKey() + "\t" + String.format("%.2f", entry.getValue()) + "\r\n");
			}
			
			writer.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
